package cn.com.infcn.monitor.task.jvm.impl;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServerConnection;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infcn.monitor.bean.IFCResponseBean;
import cn.com.infcn.monitor.jvm.bean.IFCJVMThread;
import cn.com.infcn.monitor.jvm.bean.IFCJVMThreadInfo;
import cn.com.infcn.monitor.task.impl.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;
import cn.com.infcn.monitor.util.IFCMinitorConstanst;

/**
 * Java 虚拟机线程系统的管理接口。
 * 
 * @author jjs
 *
 */
public class JVMThreadTask extends BaseTask {

	private final static Logger logger = LoggerFactory.getLogger(JVMThreadTask.class);

	public JVMThreadTask(IoSession session, Object message) {
		super(session, message);
	}

	@Override
	public void execute() throws Exception {
		IFCResponseBean result = jvmThread();
		sendResponse(result);
	}

	public IFCResponseBean jvmThread() {
		IFCResponseBean result = new IFCResponseBean();
		IFCJVMThread ifcThread = null;
		MBeanServerConnection mbs = null;
		try {
			// 获取MBeanServerConnection
			mbs = MBeanServerConnectionFactory.getInstance(IFCMinitorConstanst.JMXURL);
		} catch (IOException e) {
			logger.error("jmx.url 连接异常！" + e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg("jmx.url 连接异常！");
			return result;
		}

		try {

			// 获取ThreadMXBean对象
			ThreadMXBean threadBean = ManagementFactory.newPlatformMXBeanProxy(mbs,
					ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean.class);

			ifcThread = new IFCJVMThread();

			IFCBeanUtils.copyProperties(threadBean, ifcThread);
			ifcThread.setFindDeadlockedThreads(threadBean.findDeadlockedThreads());
			ifcThread.setFindMonitorDeadlockedThreads(threadBean.findMonitorDeadlockedThreads());

			Map<String, IFCJVMThreadInfo> ifcThreadInfoMap = new HashMap<String, IFCJVMThreadInfo>();

			long[] ids = threadBean.getAllThreadIds();
			for (long id : ids) {
				ThreadInfo threadInfo = threadBean.getThreadInfo(id);
				IFCJVMThreadInfo ifcThreadInfo = new IFCJVMThreadInfo();
				IFCBeanUtils.copyProperties(threadInfo, ifcThreadInfo);
				ifcThreadInfo.setThreadCpuTime(threadBean.getThreadCpuTime(id));
				ifcThreadInfo.setThreadUserTime(threadBean.getThreadUserTime(id));
				ifcThreadInfoMap.put(id + "", ifcThreadInfo);
			}
			IFCBeanUtils.copyProperties(threadBean, ifcThread);
			ifcThread.setThreadInfoMap(ifcThreadInfoMap);

			result.setResult(ifcThread);

			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg(e.getMessage());
		}

		return result;
	}

	public static void main(String[] args) {
		JVMThreadTask task = new JVMThreadTask(null, null);
		task.jvmThread();
	}

}
